package lib.Dao;

import lib.Model.Course;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class CourseDAO {
    public List<Course> getCourse(String se) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from "+se;
            rs = stmt.executeQuery(sql);
            while(rs.next()) {
                Course course=new Course(rs.getString("cid"),rs.getString("cname"),rs.getString("cteacher"),rs.getString("ctime"),rs.getInt("rongliang"),rs.getInt("cnum"), rs.getInt("ccount"), rs.getInt("isposted"));
                list.add(course);

            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public List<Course> getMonitorCourse(String se) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from "+se+" where ccount=1";
            rs = stmt.executeQuery(sql);
            while(rs.next()) {
                Course course=new Course(rs.getString("cid"),rs.getString("cname"),rs.getString("cteacher"),rs.getString("ctime"),rs.getInt("rongliang"),rs.getInt("cnum"), rs.getInt("ccount"), rs.getInt("isposted"));
                list.add(course);

            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Course> getCourselike(String se,String content) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            PreparedStatement pst = null;
            //System.out.println(content);
            stmt = con.createStatement();
            // INSERT INTO Student(sId,sName) VALUES (?,?)  "delete from notes where notes_id = ?";

            String sql = "select * from "+se+" where cname like  "+"'%"+content+"%'";
            //pst = con.prepareStatement(sql);
            //pst.setString(1,content);
            //System.out.println(sql);
            rs =stmt.executeQuery(sql);

            while(rs.next()) {
                Course course=new Course(rs.getString("cid"),rs.getString("cname"),rs.getString("cteacher"),rs.getString("ctime"),rs.getInt("rongliang"),rs.getInt("cnum"), rs.getInt("ccount"), rs.getInt("isposted"));
                list.add(course);

            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Course> getCid(String se,String content) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            PreparedStatement pst = null;
            //System.out.println(content);
            stmt = con.createStatement();
            // INSERT INTO Student(sId,sName) VALUES (?,?)  "delete from notes where notes_id = ?";

            String sql = "select * from "+se+" where cid like  "+"'"+content+"'";
            //pst = con.prepareStatement(sql);
            //pst.setString(1,content);
            //System.out.println(sql);
            rs =stmt.executeQuery(sql);

            while(rs.next()) {
                Course course=new Course(rs.getString("cid"),rs.getString("cname"),rs.getString("cteacher"),rs.getString("ctime"),rs.getInt("rongliang"),rs.getInt("cnum"), rs.getInt("ccount"), rs.getInt("isposted"));
                list.add(course);

            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public Course getcoursefromCid(String se,String content) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        Course course=null;
        try{
            con = dbutil.getCon();
            PreparedStatement pst = null;
            //System.out.println(content);
            stmt = con.createStatement();
            // INSERT INTO Student(sId,sName) VALUES (?,?)  "delete from notes where notes_id = ?";

            String sql = "select * from "+se+" where cid like  "+"'"+content+"'";

            rs =stmt.executeQuery(sql);

            while(rs.next()) {
                course=new Course(rs.getString("cid"),rs.getString("cname"),rs.getString("cteacher"),rs.getString("ctime"),rs.getInt("rongliang"),rs.getInt("cnum"),  rs.getInt("ccount"), rs.getInt("isposted"));

            }
            return course;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return course;
    }


    public void SetIsposted(String se,String cid,int isposted) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "update "+se+" set isposted="+isposted+" where cid="+"'"+cid+"'";
            stmt.executeUpdate(sql);

            return;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    public void SetCcount(String se,String cid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "update "+se+" set ccount=1 where cid="+"'"+cid+"'";
            stmt.executeUpdate(sql);

            return;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }
}
